package org.example.novel.security.config;

import org.example.novel.config.AnonymousConfig;
import org.example.novel.config.WebMvcConfig;
import org.example.novel.filter.JwtAuthenticationFilter;
import org.example.novel.security.handler.MyAccessDeniedHandler;
import org.example.novel.security.handler.MyAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;

import javax.sql.DataSource;

/**
 * 安全认证的配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    DataSource dataSource;

    @Autowired
    UserDetailsService userDetailsService;

    @Autowired
    JwtAuthenticationFilter jwtAuthenticationFilter;

    @Autowired
    MyAccessDeniedHandler myAccessDeniedHandler;

    @Autowired
    MyAuthenticationEntryPoint myAuthenticationEntryPoint;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .cors();

        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry
                = http.authorizeRequests();
        expressionInterceptUrlRegistry
                .antMatchers(WebMvcConfig.staticRul +"**",
                        "/api/test/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**").permitAll();
        AnonymousConfig.map.forEach((k, v)->{
            switch (k){
                case "GET":{
                    v.forEach(r->expressionInterceptUrlRegistry.antMatchers(HttpMethod.GET,r).permitAll());
                    break;
                }
                case "POST":{
                    v.forEach(r->expressionInterceptUrlRegistry.antMatchers(HttpMethod.POST,r).permitAll());
                    break;
                }
                case "DELETE":{
                    v.forEach(r->expressionInterceptUrlRegistry.antMatchers(HttpMethod.DELETE,r).permitAll());
                    break;
                }
            }
        });

        expressionInterceptUrlRegistry.anyRequest().authenticated();

        //异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(myAuthenticationEntryPoint)
                .accessDeniedHandler(myAccessDeniedHandler);

        //采用自定义过滤器
        http.addFilterAt(jwtAuthenticationFilter,UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


}
